{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "dd7cef332a62b2c3",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "# prompt template"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "175bf20b2fefb90d",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "faiss-cpu 向量数据库\n",
    "qianfan 是百度千帆大模型平台"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "initial_id",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-05-26T02:44:08.280123Z",
     "start_time": "2024-05-26T02:44:02.736417Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Note: you may need to restart the kernel to use updated packages.\n"
     ]
    }
   ],
   "source": [
    "%pip install openai==0.28.1  langchain==0.0.330 tiktoken faiss-cpu qianfan chromadb -q"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "449675d651cb360e",
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-05-26T03:58:01.434862Z",
     "start_time": "2024-05-26T03:57:39.501007Z"
    }
   },
   "outputs": [],
   "source": [
    "#设置密码\n",
    "import os\n",
    "import getpass\n",
    "\n",
    "os.environ[\"QIANFAN_AK\"] = getpass.getpass(\"输入appKey\")\n",
    "os.environ[\"QIANFAN_SK\"] = getpass.getpass(\"输入secretKey\")\n",
    "os.environ[\"OPENAI_API_KEY\"] = getpass.getpass(\"输入OpenAI Key\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f7d15d2b415e5716",
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "#提示词的模板\n",
    "from langchain import PromptTemplate\n",
    "from langchain.llms.baidu_qianfan_endpoint import QianfanLLMEndpoint\n",
    "\n",
    "llm = QianfanLLMEndpoint(model = \"Qianfan-Chinese-Llama-2-13B\")\n",
    "# 提示词，告诉大模型你的身份，需要做什么\n",
    "template = \"\"\"你作为一个经验丰富的客服代表，请为客户的问题提供解答:{Query}\"\"\"\n",
    "prompt = PromptTemplate(\n",
    "    input_variables = [\"Query\"],\n",
    "    template = template\n",
    ")\n",
    "# 真实的用户输入，如何退货，将词插入到提示模板中去\n",
    "final_prompt = prompt.format(Query = \"如何退货?\")\n",
    "print(f\"最终组合的提示词:{final_prompt}\")\n",
    "reponse = llm(final_prompt)\n",
    "print(reponse)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 根据角色生成提示词模板"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "4f4493dc19493164"
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [
    "from langchain.prompts import ChatMessagePromptTemplate\n",
    "from langchain.llms.baidu_qianfan_endpoint import QianfanLLMEndpoint\n",
    "\n",
    "llm = QianfanLLMEndpoint(model = \"Qianfan-Chinese-Llama-2-13B\")\n",
    "template = \"\"\"你作为一个经验丰富的客服代表，为用户问题提供解答:{Query}\"\"\"\n",
    "chat_message_prompt = ChatMessagePromptTemplate.from_template(role = \"技术支撑\",template = template)\n",
    "prompt_format = chat_message_prompt.format(Query=\"产品如何使用\")\n",
    "prompts = str(prompt_format)\n",
    "print(prompts)\n",
    "\n",
    "response = llm(prompts)\n",
    "print(response)\n"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "ce487d34d26a5250",
   "execution_count": null
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 模板的部分加载"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "cabb4c63307f175e"
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [
    "from langchain.prompts import PromptTemplate\n",
    "from langchain.llms.baidu_qianfan_endpoint import QianfanLLMEndpoint\n",
    "#1. 大模型的初始化\n",
    "llm = QianfanLLMEndpoint(model = \"Qianfan-Chinese-Llama-2-13B\")\n",
    "\n",
    "#2. 创建提示词模板\n",
    "# 通过product 占位符表示用户想咨询什么产品\n",
    "# 占位符problem 表示用户想问什么问题\n",
    "prompt = PromptTemplate(template =\"我想知道关于{product}的{problem}问题\",input_variables=[\"product\",\"problem\"])\n",
    "\n",
    "#3.用户第一次输入，告诉大模型模板，我想咨询的产品\n",
    "partial_prompt = prompt.partial(product = \"手机\")\n",
    "\n",
    "#4 用户的第二次输入，告诉大模型模版， 我知道如何恢复出厂设置，追加\n",
    "final_prompt = partial_prompt.format(problem = \"如何恢复出厂设置\")\n",
    "\n",
    "print(final_prompt)\n",
    "\n",
    "#5.执行大模型\n",
    "reponse = llm.generate([final_prompt])\n",
    "print(reponse)\n"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "37101edc264b2623",
   "execution_count": null
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 多模版合并"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "60a0e1eabf8b3df"
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [
    "# 通过管道方式让多模版按照一定顺序执行\n",
    "from langchain.prompts.pipeline import PipelinePromptTemplate\n",
    "from langchain.prompts.prompt import PromptTemplate\n",
    "from langchain.llms.baidu_qianfan_endpoint import QianfanLLMEndpoint\n",
    "\n",
    "llm = QianfanLLMEndpoint(model = \"Qianfan-Chinese-Llama-2-13B\")\n",
    "\n",
    "full_template = \"\"\"{category_select} {brand_select} {os_select} {problem_select}\"\"\"\n",
    "full_prompt = PromptTemplate.from_template(full_template)\n",
    "\n",
    "category_template = \"\"\"你好，我是{personal}\"\"\"\n",
    "category_prompt = PromptTemplate.from_template(category_template)\n",
    "\n",
    "brand_template = \"\"\"我了解您使用的是 {brand}手机\"\"\"\n",
    "brand_prompt = PromptTemplate.from_template(brand_template)\n",
    "\n",
    "os_template = \"\"\"操作系统为{os}\"\"\"\n",
    "os_prompt = PromptTemplate.from_template(os_template)\n",
    "\n",
    "problem_template = \"\"\"您遇到了{problem}的问题， 我可以为您解决\"\"\"\n",
    "problem_prompt = PromptTemplate.from_template(problem_template)\n",
    "\n",
    "input_prompts = [\n",
    "    (\"category_select\",category_prompt),\n",
    "    (\"brand_select\",brand_prompt),\n",
    "    (\"os_select\",os_prompt),\n",
    "    (\"problem_select\",problem_prompt),\n",
    "]\n",
    "\n",
    "pipeline_prompt = PipelinePromptTemplate(final_prompt = full_prompt,pipeline_prompts = input_prompts)\n",
    "\n",
    "formatted_prompt = pipeline_prompt.format(\n",
    "    personal = \"技术支持\",\n",
    "    brand = \"华为\",\n",
    "    os = \"HarmonyOS\",\n",
    "    problem = \"恢复出厂设置\"\n",
    ")\n",
    "# response = llm.generate([str(formatted_prompt)])\n",
    "response = llm(str(formatted_prompt))\n",
    "print(response)"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "769fb6edc4323915",
   "execution_count": null
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [
    "# %pip install --upgrade openai"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "e32cbfeb09d6ecb8",
   "execution_count": null
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [
    "!pip3 install diffusers transformers accelerate torch openai langchain\n"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "f20256453ebeef84",
   "execution_count": null
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [
    "import os\n",
    "import getpass\n",
    "from openai import OpenAI\n",
    "os.environ[\"OPENAI_API_KEY\"] = getpass.getpass(\"输入OpenAI Key\")\n",
    "client = OpenAI()\n",
    "\n",
    "completion = client.chat.completions.create(\n",
    "  model=\"gpt-3.5-turbo\",\n",
    "  messages=[\n",
    "    {\"role\": \"system\", \"content\": \"You are a poetic assistant, skilled in explaining complex programming concepts with creative flair.\"},\n",
    "    {\"role\": \"user\", \"content\": \"Compose a poem that explains the concept of recursion in programming.\"}\n",
    "  ]\n",
    ")\n",
    "\n",
    "print(completion.choices[0].message)"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "f2491a42f28850f6",
   "execution_count": null
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [
    "import os\n",
    "import getpass\n",
    "from openai import OpenAI\n",
    "os.environ[\"OPENAI_API_KEY\"] = getpass.getpass(\"输入OpenAI Key\")\n",
    "client = OpenAI()\n",
    "\n",
    "response = client.images.generate(\n",
    "  model=\"dall-e-3\",\n",
    "  prompt=\"a yellow siamese cat\",\n",
    "  size=\"1024x1024\",\n",
    "  quality=\"standard\",\n",
    "  n=1,\n",
    "  response_format=\"b64_json\"\n",
    ")\n",
    "\n",
    "\n",
    "image_url = response.data[0].url\n",
    "image_json = response.data[0].b64_json;\n",
    "reversed_prompt = response.data[0].revised_prompt;\n",
    "\n",
    "# for image in response.data[0]:\n",
    "#     with open(\"./cat.png\", \"wb\") as f:\n",
    "#         f.write(image.read())\n",
    "\n",
    "print(image_url)\n",
    "print(image_json)\n",
    "print(reversed_prompt)\n"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "e31907172241b4cf",
   "execution_count": null
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 示例"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "d85297e081e511ea"
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [
    "from langchain.prompts import PromptTemplate\n",
    "from langchain.prompts import FewShotPromptTemplate\n",
    "from langchain.prompts.example_selector import LengthBasedExampleSelector\n",
    "from langchain.llms.baidu_qianfan_endpoint import QianfanLLMEndpoint\n",
    "\n",
    "llm = QianfanLLMEndpoint(model = \"Qianfan-Chinese-Llama-2-13B\")\n",
    "\n",
    "#在自动客服系统中， 当用户提问之后， 客服人员都会先回答问题，再确认问题是否帮助到客户了。\n",
    "examples= [\n",
    "    {\"input\": \"如何重置密码？\", \"output\": \"请前往登录页面，点击“忘记密码”链接，然后按照提示操作。这样您可以收到一个重置密码的链接，通过链接设置新的密码。是否解决了您的问题？\"},\n",
    "    {\"input\": \"怎样查询订单状态？\", \"output\": \"请登录您的账户，然后点击“我的订单”查看订单状态。在“我的订单”页面，您可以看到订单的当前状态，例如“处理中”，“已发货”或“已完成”。这对您有帮助吗？\"},\n",
    "    {\"input\": \"如何申请退货？\", \"output\": \"请登录您的账户，找到相应的订单，点击“申请退货”按钮。系统将引导您完成退货申请流程，包括提供退货理由和退货地址。需要我为您提供其他帮助吗？\"},\n",
    "    {\"input\": \"如何使用优惠券？\", \"output\": \"在结账页面，找到“使用优惠券”选项，输入优惠券代码。优惠券将会立即生效，您可以看到总价已经调整。还有其他我可以为您做的吗？\"},\n",
    "]\n",
    "examples_prompt = PromptTemplate(\n",
    "    input_variables=[\"input\",\"output\"],\n",
    "    template=\"问题:{input} \\n 回答:{output}\"\n",
    ")\n",
    "\n",
    "# 给出的提示和prompt相关联\n",
    "examples_selector = LengthBasedExampleSelector(\n",
    "    examples=examples,\n",
    "    example_prompt = examples_prompt\n",
    ")\n",
    "\n",
    "#生成机遇的example的prompt_template\n",
    "dynamic_prompt = FewShotPromptTemplate(\n",
    "    example_selector = examples_selector,\n",
    "    example_prompt = examples_prompt,\n",
    "    prefix=\"回答以下客户的问题， 并提供额外的解释或者信息， 然后询问他们是否满意。\",\n",
    "    suffix=\"问题:{query}\\n回答:\",\n",
    "    input_variables = [\"input\"]\n",
    ")\n",
    "\n",
    "print(dynamic_prompt.format(query=\"如何取消订单?\"))\n",
    "\n",
    "response = llm.generate([dynamic_prompt.format(query=\"如何取消订单\")])\n",
    "print(response)\n",
    "\n"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "290adc2d0ce5ed82",
   "execution_count": null
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 用户请求归类"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "45e92ff7e6bfa851"
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "[INFO] [05-26 10:45:08] oauth.py:228 [t:8561991168]: trying to refresh access_token for ak `jRLjYU***`\n",
      "[INFO] [05-26 10:45:08] oauth.py:243 [t:8561991168]: sucessfully refresh access_token\n",
      "[ERROR] [05-26 10:45:08] openapi_requestor.py:256 [t:8561991168]: api request req_id:  failed with error code: 17, err msg: Open api daily request limit reached 可能的原因: 未开通所调用服务的付费权限，或者账户已欠费, please check https://cloud.baidu.com/doc/WENXINWORKSHOP/s/tlmyncueh\n"
     ]
    },
    {
     "ename": "APIError",
     "evalue": "api return error, req_id:  code: 17, msg: Open api daily request limit reached 可能的原因: 未开通所调用服务的付费权限，或者账户已欠费",
     "output_type": "error",
     "traceback": [
      "\u001B[0;31m---------------------------------------------------------------------------\u001B[0m",
      "\u001B[0;31mAPIError\u001B[0m                                  Traceback (most recent call last)",
      "Cell \u001B[0;32mIn[3], line 28\u001B[0m\n\u001B[1;32m     10\u001B[0m examples \u001B[38;5;241m=\u001B[39m [\n\u001B[1;32m     11\u001B[0m     {\u001B[38;5;124m\"\u001B[39m\u001B[38;5;124minput\u001B[39m\u001B[38;5;124m\"\u001B[39m: \u001B[38;5;124m\"\u001B[39m\u001B[38;5;124m我忘记了密码，怎么重置？\u001B[39m\u001B[38;5;124m\"\u001B[39m, \u001B[38;5;124m\"\u001B[39m\u001B[38;5;124moutput\u001B[39m\u001B[38;5;124m\"\u001B[39m: \u001B[38;5;124m\"\u001B[39m\u001B[38;5;124m账户问题\u001B[39m\u001B[38;5;124m\"\u001B[39m},\n\u001B[1;32m     12\u001B[0m     {\u001B[38;5;124m\"\u001B[39m\u001B[38;5;124minput\u001B[39m\u001B[38;5;124m\"\u001B[39m: \u001B[38;5;124m\"\u001B[39m\u001B[38;5;124m我的订单在哪里，已经过了一周了？\u001B[39m\u001B[38;5;124m\"\u001B[39m, \u001B[38;5;124m\"\u001B[39m\u001B[38;5;124moutput\u001B[39m\u001B[38;5;124m\"\u001B[39m: \u001B[38;5;124m\"\u001B[39m\u001B[38;5;124m订单问题\u001B[39m\u001B[38;5;124m\"\u001B[39m},\n\u001B[0;32m   (...)\u001B[0m\n\u001B[1;32m     20\u001B[0m     {\u001B[38;5;124m\"\u001B[39m\u001B[38;5;124minput\u001B[39m\u001B[38;5;124m\"\u001B[39m: \u001B[38;5;124m\"\u001B[39m\u001B[38;5;124m我无法登录我的账户，我该怎么办？\u001B[39m\u001B[38;5;124m\"\u001B[39m, \u001B[38;5;124m\"\u001B[39m\u001B[38;5;124moutput\u001B[39m\u001B[38;5;124m\"\u001B[39m: \u001B[38;5;124m\"\u001B[39m\u001B[38;5;124m账户问题\u001B[39m\u001B[38;5;124m\"\u001B[39m}\n\u001B[1;32m     21\u001B[0m ]\n\u001B[1;32m     23\u001B[0m example_prompt \u001B[38;5;241m=\u001B[39m PromptTemplate(\n\u001B[1;32m     24\u001B[0m     input_variables\u001B[38;5;241m=\u001B[39m [\u001B[38;5;124m\"\u001B[39m\u001B[38;5;124minput\u001B[39m\u001B[38;5;124m\"\u001B[39m, \u001B[38;5;124m\"\u001B[39m\u001B[38;5;124moutput\u001B[39m\u001B[38;5;124m\"\u001B[39m],\n\u001B[1;32m     25\u001B[0m     template\u001B[38;5;241m=\u001B[39m \u001B[38;5;124m\"\u001B[39m\u001B[38;5;124m示例输入:\u001B[39m\u001B[38;5;132;01m{input}\u001B[39;00m\u001B[38;5;130;01m\\n\u001B[39;00m\u001B[38;5;124m 示例输出:\u001B[39m\u001B[38;5;132;01m{output}\u001B[39;00m\u001B[38;5;124m\"\u001B[39m,\n\u001B[1;32m     26\u001B[0m )\n\u001B[0;32m---> 28\u001B[0m exmaple_selector \u001B[38;5;241m=\u001B[39m \u001B[43mSemanticSimilarityExampleSelector\u001B[49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[43mfrom_examples\u001B[49m\u001B[43m(\u001B[49m\n\u001B[1;32m     29\u001B[0m \u001B[43m    \u001B[49m\u001B[43mexamples\u001B[49m\u001B[43m,\u001B[49m\n\u001B[1;32m     30\u001B[0m \u001B[43m    \u001B[49m\u001B[43mQianfanEmbeddingsEndpoint\u001B[49m\u001B[43m(\u001B[49m\u001B[43m)\u001B[49m\u001B[43m,\u001B[49m\n\u001B[1;32m     31\u001B[0m \u001B[43m    \u001B[49m\u001B[43mFAISS\u001B[49m\u001B[43m,\u001B[49m\n\u001B[1;32m     32\u001B[0m \u001B[43m    \u001B[49m\u001B[43mk\u001B[49m\u001B[38;5;241;43m=\u001B[39;49m\u001B[38;5;241;43m2\u001B[39;49m\n\u001B[1;32m     33\u001B[0m \u001B[43m)\u001B[49m\n\u001B[1;32m     35\u001B[0m similar_prompt\u001B[38;5;241m=\u001B[39mFewShotPromptTemplate(\n\u001B[1;32m     36\u001B[0m     example_selector\u001B[38;5;241m=\u001B[39m exmaple_selector,\n\u001B[1;32m     37\u001B[0m     example_prompt \u001B[38;5;241m=\u001B[39m example_prompt,\n\u001B[0;32m   (...)\u001B[0m\n\u001B[1;32m     40\u001B[0m     input_variables\u001B[38;5;241m=\u001B[39m[\u001B[38;5;124m\"\u001B[39m\u001B[38;5;124mquery\u001B[39m\u001B[38;5;124m\"\u001B[39m],\n\u001B[1;32m     41\u001B[0m )\n\u001B[1;32m     42\u001B[0m my_query \u001B[38;5;241m=\u001B[39m \u001B[38;5;124m\"\u001B[39m\u001B[38;5;124m我想知道我的订单何时能够到达？\u001B[39m\u001B[38;5;124m\"\u001B[39m\n",
      "File \u001B[0;32m/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages/langchain/prompts/example_selector/semantic_similarity.py:95\u001B[0m, in \u001B[0;36mSemanticSimilarityExampleSelector.from_examples\u001B[0;34m(cls, examples, embeddings, vectorstore_cls, k, input_keys, **vectorstore_cls_kwargs)\u001B[0m\n\u001B[1;32m     93\u001B[0m \u001B[38;5;28;01melse\u001B[39;00m:\n\u001B[1;32m     94\u001B[0m     string_examples \u001B[38;5;241m=\u001B[39m [\u001B[38;5;124m\"\u001B[39m\u001B[38;5;124m \u001B[39m\u001B[38;5;124m\"\u001B[39m\u001B[38;5;241m.\u001B[39mjoin(sorted_values(eg)) \u001B[38;5;28;01mfor\u001B[39;00m eg \u001B[38;5;129;01min\u001B[39;00m examples]\n\u001B[0;32m---> 95\u001B[0m vectorstore \u001B[38;5;241m=\u001B[39m \u001B[43mvectorstore_cls\u001B[49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[43mfrom_texts\u001B[49m\u001B[43m(\u001B[49m\n\u001B[1;32m     96\u001B[0m \u001B[43m    \u001B[49m\u001B[43mstring_examples\u001B[49m\u001B[43m,\u001B[49m\u001B[43m \u001B[49m\u001B[43membeddings\u001B[49m\u001B[43m,\u001B[49m\u001B[43m \u001B[49m\u001B[43mmetadatas\u001B[49m\u001B[38;5;241;43m=\u001B[39;49m\u001B[43mexamples\u001B[49m\u001B[43m,\u001B[49m\u001B[43m \u001B[49m\u001B[38;5;241;43m*\u001B[39;49m\u001B[38;5;241;43m*\u001B[39;49m\u001B[43mvectorstore_cls_kwargs\u001B[49m\n\u001B[1;32m     97\u001B[0m \u001B[43m\u001B[49m\u001B[43m)\u001B[49m\n\u001B[1;32m     98\u001B[0m \u001B[38;5;28;01mreturn\u001B[39;00m \u001B[38;5;28mcls\u001B[39m(vectorstore\u001B[38;5;241m=\u001B[39mvectorstore, k\u001B[38;5;241m=\u001B[39mk, input_keys\u001B[38;5;241m=\u001B[39minput_keys)\n",
      "File \u001B[0;32m/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages/langchain/vectorstores/faiss.py:911\u001B[0m, in \u001B[0;36mFAISS.from_texts\u001B[0;34m(cls, texts, embedding, metadatas, ids, **kwargs)\u001B[0m\n\u001B[1;32m    884\u001B[0m \u001B[38;5;129m@classmethod\u001B[39m\n\u001B[1;32m    885\u001B[0m \u001B[38;5;28;01mdef\u001B[39;00m \u001B[38;5;21mfrom_texts\u001B[39m(\n\u001B[1;32m    886\u001B[0m     \u001B[38;5;28mcls\u001B[39m,\n\u001B[0;32m   (...)\u001B[0m\n\u001B[1;32m    891\u001B[0m     \u001B[38;5;241m*\u001B[39m\u001B[38;5;241m*\u001B[39mkwargs: Any,\n\u001B[1;32m    892\u001B[0m ) \u001B[38;5;241m-\u001B[39m\u001B[38;5;241m>\u001B[39m FAISS:\n\u001B[1;32m    893\u001B[0m \u001B[38;5;250m    \u001B[39m\u001B[38;5;124;03m\"\"\"Construct FAISS wrapper from raw documents.\u001B[39;00m\n\u001B[1;32m    894\u001B[0m \n\u001B[1;32m    895\u001B[0m \u001B[38;5;124;03m    This is a user friendly interface that:\u001B[39;00m\n\u001B[0;32m   (...)\u001B[0m\n\u001B[1;32m    909\u001B[0m \u001B[38;5;124;03m            faiss = FAISS.from_texts(texts, embeddings)\u001B[39;00m\n\u001B[1;32m    910\u001B[0m \u001B[38;5;124;03m    \"\"\"\u001B[39;00m\n\u001B[0;32m--> 911\u001B[0m     embeddings \u001B[38;5;241m=\u001B[39m \u001B[43membedding\u001B[49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[43membed_documents\u001B[49m\u001B[43m(\u001B[49m\u001B[43mtexts\u001B[49m\u001B[43m)\u001B[49m\n\u001B[1;32m    912\u001B[0m     \u001B[38;5;28;01mreturn\u001B[39;00m \u001B[38;5;28mcls\u001B[39m\u001B[38;5;241m.\u001B[39m__from(\n\u001B[1;32m    913\u001B[0m         texts,\n\u001B[1;32m    914\u001B[0m         embeddings,\n\u001B[0;32m   (...)\u001B[0m\n\u001B[1;32m    918\u001B[0m         \u001B[38;5;241m*\u001B[39m\u001B[38;5;241m*\u001B[39mkwargs,\n\u001B[1;32m    919\u001B[0m     )\n",
      "File \u001B[0;32m/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages/langchain/embeddings/baidu_qianfan_endpoint.py:120\u001B[0m, in \u001B[0;36mQianfanEmbeddingsEndpoint.embed_documents\u001B[0;34m(self, texts)\u001B[0m\n\u001B[1;32m    118\u001B[0m lst \u001B[38;5;241m=\u001B[39m []\n\u001B[1;32m    119\u001B[0m \u001B[38;5;28;01mfor\u001B[39;00m chunk \u001B[38;5;129;01min\u001B[39;00m text_in_chunks:\n\u001B[0;32m--> 120\u001B[0m     resp \u001B[38;5;241m=\u001B[39m \u001B[38;5;28;43mself\u001B[39;49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[43mclient\u001B[49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[43mdo\u001B[49m\u001B[43m(\u001B[49m\u001B[43mtexts\u001B[49m\u001B[38;5;241;43m=\u001B[39;49m\u001B[43mchunk\u001B[49m\u001B[43m)\u001B[49m\n\u001B[1;32m    121\u001B[0m     lst\u001B[38;5;241m.\u001B[39mextend([res[\u001B[38;5;124m\"\u001B[39m\u001B[38;5;124membedding\u001B[39m\u001B[38;5;124m\"\u001B[39m] \u001B[38;5;28;01mfor\u001B[39;00m res \u001B[38;5;129;01min\u001B[39;00m resp[\u001B[38;5;124m\"\u001B[39m\u001B[38;5;124mdata\u001B[39m\u001B[38;5;124m\"\u001B[39m]])\n\u001B[1;32m    122\u001B[0m \u001B[38;5;28;01mreturn\u001B[39;00m lst\n",
      "File \u001B[0;32m/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages/qianfan/resources/llm/embedding.py:172\u001B[0m, in \u001B[0;36mEmbedding.do\u001B[0;34m(self, texts, model, endpoint, stream, retry_count, request_timeout, request_id, backoff_factor, **kwargs)\u001B[0m\n\u001B[1;32m    169\u001B[0m \u001B[38;5;28;01mif\u001B[39;00m request_id \u001B[38;5;129;01mis\u001B[39;00m \u001B[38;5;129;01mnot\u001B[39;00m \u001B[38;5;28;01mNone\u001B[39;00m:\n\u001B[1;32m    170\u001B[0m     kwargs[\u001B[38;5;124m\"\u001B[39m\u001B[38;5;124mrequest_id\u001B[39m\u001B[38;5;124m\"\u001B[39m] \u001B[38;5;241m=\u001B[39m request_id\n\u001B[0;32m--> 172\u001B[0m \u001B[38;5;28;01mreturn\u001B[39;00m \u001B[38;5;28;43mself\u001B[39;49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[43m_do\u001B[49m\u001B[43m(\u001B[49m\n\u001B[1;32m    173\u001B[0m \u001B[43m    \u001B[49m\u001B[43mmodel\u001B[49m\u001B[43m,\u001B[49m\n\u001B[1;32m    174\u001B[0m \u001B[43m    \u001B[49m\u001B[43mendpoint\u001B[49m\u001B[43m,\u001B[49m\n\u001B[1;32m    175\u001B[0m \u001B[43m    \u001B[49m\u001B[43mstream\u001B[49m\u001B[43m,\u001B[49m\n\u001B[1;32m    176\u001B[0m \u001B[43m    \u001B[49m\u001B[43mretry_count\u001B[49m\u001B[43m,\u001B[49m\n\u001B[1;32m    177\u001B[0m \u001B[43m    \u001B[49m\u001B[43mrequest_timeout\u001B[49m\u001B[43m,\u001B[49m\n\u001B[1;32m    178\u001B[0m \u001B[43m    \u001B[49m\u001B[43mbackoff_factor\u001B[49m\u001B[43m,\u001B[49m\n\u001B[1;32m    179\u001B[0m \u001B[43m    \u001B[49m\u001B[38;5;241;43m*\u001B[39;49m\u001B[38;5;241;43m*\u001B[39;49m\u001B[43mkwargs\u001B[49m\u001B[43m,\u001B[49m\n\u001B[1;32m    180\u001B[0m \u001B[43m\u001B[49m\u001B[43m)\u001B[49m\n",
      "File \u001B[0;32m/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages/qianfan/resources/llm/base.py:290\u001B[0m, in \u001B[0;36mBaseResource._do\u001B[0;34m(self, model, endpoint, stream, retry_count, request_timeout, backoff_factor, retry_jitter, retry_err_codes, retry_max_wait_interval, **kwargs)\u001B[0m\n\u001B[1;32m    288\u001B[0m             \u001B[38;5;28;01mcontinue\u001B[39;00m\n\u001B[1;32m    289\u001B[0m         \u001B[38;5;28;01melse\u001B[39;00m:\n\u001B[0;32m--> 290\u001B[0m             \u001B[38;5;28;01mraise\u001B[39;00m e\n\u001B[1;32m    291\u001B[0m     \u001B[38;5;28;01mbreak\u001B[39;00m\n\u001B[1;32m    293\u001B[0m \u001B[38;5;28;01mreturn\u001B[39;00m resp\n",
      "File \u001B[0;32m/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages/qianfan/resources/llm/base.py:271\u001B[0m, in \u001B[0;36mBaseResource._do\u001B[0;34m(self, model, endpoint, stream, retry_count, request_timeout, backoff_factor, retry_jitter, retry_err_codes, retry_max_wait_interval, **kwargs)\u001B[0m\n\u001B[1;32m    269\u001B[0m \u001B[38;5;28;01mwhile\u001B[39;00m \u001B[38;5;28;01mTrue\u001B[39;00m:\n\u001B[1;32m    270\u001B[0m     \u001B[38;5;28;01mtry\u001B[39;00m:\n\u001B[0;32m--> 271\u001B[0m         resp \u001B[38;5;241m=\u001B[39m \u001B[38;5;28;43mself\u001B[39;49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[43m_client\u001B[49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[43mllm\u001B[49m\u001B[43m(\u001B[49m\n\u001B[1;32m    272\u001B[0m \u001B[43m            \u001B[49m\u001B[43mendpoint\u001B[49m\u001B[38;5;241;43m=\u001B[39;49m\u001B[43mendpoint\u001B[49m\u001B[43m,\u001B[49m\n\u001B[1;32m    273\u001B[0m \u001B[43m            \u001B[49m\u001B[43mheader\u001B[49m\u001B[38;5;241;43m=\u001B[39;49m\u001B[38;5;28;43mself\u001B[39;49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[43m_generate_header\u001B[49m\u001B[43m(\u001B[49m\u001B[43mmodel\u001B[49m\u001B[43m,\u001B[49m\u001B[43m \u001B[49m\u001B[43mendpoint\u001B[49m\u001B[43m,\u001B[49m\u001B[43m \u001B[49m\u001B[43mstream\u001B[49m\u001B[43m,\u001B[49m\u001B[43m \u001B[49m\u001B[38;5;241;43m*\u001B[39;49m\u001B[38;5;241;43m*\u001B[39;49m\u001B[43mkwargs\u001B[49m\u001B[43m)\u001B[49m\u001B[43m,\u001B[49m\n\u001B[1;32m    274\u001B[0m \u001B[43m            \u001B[49m\u001B[43mquery\u001B[49m\u001B[38;5;241;43m=\u001B[39;49m\u001B[38;5;28;43mself\u001B[39;49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[43m_generate_query\u001B[49m\u001B[43m(\u001B[49m\u001B[43mmodel\u001B[49m\u001B[43m,\u001B[49m\u001B[43m \u001B[49m\u001B[43mendpoint\u001B[49m\u001B[43m,\u001B[49m\u001B[43m \u001B[49m\u001B[43mstream\u001B[49m\u001B[43m,\u001B[49m\u001B[43m \u001B[49m\u001B[38;5;241;43m*\u001B[39;49m\u001B[38;5;241;43m*\u001B[39;49m\u001B[43mkwargs\u001B[49m\u001B[43m)\u001B[49m\u001B[43m,\u001B[49m\n\u001B[1;32m    275\u001B[0m \u001B[43m            \u001B[49m\u001B[43mbody\u001B[49m\u001B[38;5;241;43m=\u001B[39;49m\u001B[38;5;28;43mself\u001B[39;49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[43m_generate_body\u001B[49m\u001B[43m(\u001B[49m\u001B[43mmodel\u001B[49m\u001B[43m,\u001B[49m\u001B[43m \u001B[49m\u001B[43mendpoint\u001B[49m\u001B[43m,\u001B[49m\u001B[43m \u001B[49m\u001B[43mstream\u001B[49m\u001B[43m,\u001B[49m\u001B[43m \u001B[49m\u001B[38;5;241;43m*\u001B[39;49m\u001B[38;5;241;43m*\u001B[39;49m\u001B[43mkwargs\u001B[49m\u001B[43m)\u001B[49m\u001B[43m,\u001B[49m\n\u001B[1;32m    276\u001B[0m \u001B[43m            \u001B[49m\u001B[43mstream\u001B[49m\u001B[38;5;241;43m=\u001B[39;49m\u001B[43mstream\u001B[49m\u001B[43m,\u001B[49m\n\u001B[1;32m    277\u001B[0m \u001B[43m            \u001B[49m\u001B[43mdata_postprocess\u001B[49m\u001B[38;5;241;43m=\u001B[39;49m\u001B[38;5;28;43mself\u001B[39;49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[43m_data_postprocess\u001B[49m\u001B[43m,\u001B[49m\n\u001B[1;32m    278\u001B[0m \u001B[43m            \u001B[49m\u001B[43mretry_config\u001B[49m\u001B[38;5;241;43m=\u001B[39;49m\u001B[43mretry_config\u001B[49m\u001B[43m,\u001B[49m\n\u001B[1;32m    279\u001B[0m \u001B[43m        \u001B[49m\u001B[43m)\u001B[49m\n\u001B[1;32m    280\u001B[0m     \u001B[38;5;28;01mexcept\u001B[39;00m errors\u001B[38;5;241m.\u001B[39mAPIError \u001B[38;5;28;01mas\u001B[39;00m e:\n\u001B[1;32m    281\u001B[0m         \u001B[38;5;28;01mif\u001B[39;00m (\n\u001B[1;32m    282\u001B[0m             e\u001B[38;5;241m.\u001B[39merror_code \u001B[38;5;241m==\u001B[39m APIErrorCode\u001B[38;5;241m.\u001B[39mUnsupportedMethod\n\u001B[1;32m    283\u001B[0m             \u001B[38;5;129;01mand\u001B[39;00m \u001B[38;5;129;01mnot\u001B[39;00m refreshed_model_list\n\u001B[1;32m    284\u001B[0m         ):\n",
      "File \u001B[0;32m/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages/qianfan/resources/requestor/openapi_requestor.py:416\u001B[0m, in \u001B[0;36mQfAPIRequestor.llm\u001B[0;34m(self, endpoint, header, query, body, stream, data_postprocess, retry_config)\u001B[0m\n\u001B[1;32m    411\u001B[0m     \u001B[38;5;28;01melse\u001B[39;00m:\n\u001B[1;32m    412\u001B[0m         \u001B[38;5;28;01mreturn\u001B[39;00m \u001B[38;5;28mself\u001B[39m\u001B[38;5;241m.\u001B[39m_compensate_token_usage_non_stream(\n\u001B[1;32m    413\u001B[0m             \u001B[38;5;28mself\u001B[39m\u001B[38;5;241m.\u001B[39m_request(req, data_postprocess\u001B[38;5;241m=\u001B[39mdata_postprocess), token_count\n\u001B[1;32m    414\u001B[0m         )\n\u001B[0;32m--> 416\u001B[0m \u001B[38;5;28;01mreturn\u001B[39;00m \u001B[38;5;28;43mself\u001B[39;49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[43m_with_retry\u001B[49m\u001B[43m(\u001B[49m\u001B[43mretry_config\u001B[49m\u001B[43m,\u001B[49m\u001B[43m \u001B[49m\u001B[43m_helper\u001B[49m\u001B[43m)\u001B[49m\n",
      "File \u001B[0;32m/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages/qianfan/resources/requestor/base.py:419\u001B[0m, in \u001B[0;36mBaseAPIRequestor._with_retry\u001B[0;34m(self, config, func, *args)\u001B[0m\n\u001B[1;32m    406\u001B[0m \u001B[38;5;129m@retry\u001B[39m(\n\u001B[1;32m    407\u001B[0m     wait\u001B[38;5;241m=\u001B[39mwait_exponential_jitter(\n\u001B[1;32m    408\u001B[0m         initial\u001B[38;5;241m=\u001B[39mconfig\u001B[38;5;241m.\u001B[39mbackoff_factor,\n\u001B[0;32m   (...)\u001B[0m\n\u001B[1;32m    415\u001B[0m )\n\u001B[1;32m    416\u001B[0m \u001B[38;5;28;01mdef\u001B[39;00m \u001B[38;5;21m_retry_wrapper\u001B[39m(\u001B[38;5;241m*\u001B[39margs: Any) \u001B[38;5;241m-\u001B[39m\u001B[38;5;241m>\u001B[39m _T:\n\u001B[1;32m    417\u001B[0m     \u001B[38;5;28;01mreturn\u001B[39;00m func(\u001B[38;5;241m*\u001B[39margs)\n\u001B[0;32m--> 419\u001B[0m \u001B[38;5;28;01mreturn\u001B[39;00m \u001B[43m_retry_wrapper\u001B[49m\u001B[43m(\u001B[49m\u001B[38;5;241;43m*\u001B[39;49m\u001B[43margs\u001B[49m\u001B[43m)\u001B[49m\n",
      "File \u001B[0;32m/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages/tenacity/__init__.py:330\u001B[0m, in \u001B[0;36mBaseRetrying.wraps.<locals>.wrapped_f\u001B[0;34m(*args, **kw)\u001B[0m\n\u001B[1;32m    326\u001B[0m \u001B[38;5;129m@functools\u001B[39m\u001B[38;5;241m.\u001B[39mwraps(\n\u001B[1;32m    327\u001B[0m     f, functools\u001B[38;5;241m.\u001B[39mWRAPPER_ASSIGNMENTS \u001B[38;5;241m+\u001B[39m (\u001B[38;5;124m\"\u001B[39m\u001B[38;5;124m__defaults__\u001B[39m\u001B[38;5;124m\"\u001B[39m, \u001B[38;5;124m\"\u001B[39m\u001B[38;5;124m__kwdefaults__\u001B[39m\u001B[38;5;124m\"\u001B[39m)\n\u001B[1;32m    328\u001B[0m )\n\u001B[1;32m    329\u001B[0m \u001B[38;5;28;01mdef\u001B[39;00m \u001B[38;5;21mwrapped_f\u001B[39m(\u001B[38;5;241m*\u001B[39margs: t\u001B[38;5;241m.\u001B[39mAny, \u001B[38;5;241m*\u001B[39m\u001B[38;5;241m*\u001B[39mkw: t\u001B[38;5;241m.\u001B[39mAny) \u001B[38;5;241m-\u001B[39m\u001B[38;5;241m>\u001B[39m t\u001B[38;5;241m.\u001B[39mAny:\n\u001B[0;32m--> 330\u001B[0m     \u001B[38;5;28;01mreturn\u001B[39;00m \u001B[38;5;28;43mself\u001B[39;49m\u001B[43m(\u001B[49m\u001B[43mf\u001B[49m\u001B[43m,\u001B[49m\u001B[43m \u001B[49m\u001B[38;5;241;43m*\u001B[39;49m\u001B[43margs\u001B[49m\u001B[43m,\u001B[49m\u001B[43m \u001B[49m\u001B[38;5;241;43m*\u001B[39;49m\u001B[38;5;241;43m*\u001B[39;49m\u001B[43mkw\u001B[49m\u001B[43m)\u001B[49m\n",
      "File \u001B[0;32m/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages/tenacity/__init__.py:467\u001B[0m, in \u001B[0;36mRetrying.__call__\u001B[0;34m(self, fn, *args, **kwargs)\u001B[0m\n\u001B[1;32m    465\u001B[0m retry_state \u001B[38;5;241m=\u001B[39m RetryCallState(retry_object\u001B[38;5;241m=\u001B[39m\u001B[38;5;28mself\u001B[39m, fn\u001B[38;5;241m=\u001B[39mfn, args\u001B[38;5;241m=\u001B[39margs, kwargs\u001B[38;5;241m=\u001B[39mkwargs)\n\u001B[1;32m    466\u001B[0m \u001B[38;5;28;01mwhile\u001B[39;00m \u001B[38;5;28;01mTrue\u001B[39;00m:\n\u001B[0;32m--> 467\u001B[0m     do \u001B[38;5;241m=\u001B[39m \u001B[38;5;28;43mself\u001B[39;49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[43miter\u001B[49m\u001B[43m(\u001B[49m\u001B[43mretry_state\u001B[49m\u001B[38;5;241;43m=\u001B[39;49m\u001B[43mretry_state\u001B[49m\u001B[43m)\u001B[49m\n\u001B[1;32m    468\u001B[0m     \u001B[38;5;28;01mif\u001B[39;00m \u001B[38;5;28misinstance\u001B[39m(do, DoAttempt):\n\u001B[1;32m    469\u001B[0m         \u001B[38;5;28;01mtry\u001B[39;00m:\n",
      "File \u001B[0;32m/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages/tenacity/__init__.py:368\u001B[0m, in \u001B[0;36mBaseRetrying.iter\u001B[0;34m(self, retry_state)\u001B[0m\n\u001B[1;32m    366\u001B[0m result \u001B[38;5;241m=\u001B[39m \u001B[38;5;28;01mNone\u001B[39;00m\n\u001B[1;32m    367\u001B[0m \u001B[38;5;28;01mfor\u001B[39;00m action \u001B[38;5;129;01min\u001B[39;00m \u001B[38;5;28mself\u001B[39m\u001B[38;5;241m.\u001B[39miter_state\u001B[38;5;241m.\u001B[39mactions:\n\u001B[0;32m--> 368\u001B[0m     result \u001B[38;5;241m=\u001B[39m \u001B[43maction\u001B[49m\u001B[43m(\u001B[49m\u001B[43mretry_state\u001B[49m\u001B[43m)\u001B[49m\n\u001B[1;32m    369\u001B[0m \u001B[38;5;28;01mreturn\u001B[39;00m result\n",
      "File \u001B[0;32m/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages/tenacity/__init__.py:390\u001B[0m, in \u001B[0;36mBaseRetrying._post_retry_check_actions.<locals>.<lambda>\u001B[0;34m(rs)\u001B[0m\n\u001B[1;32m    388\u001B[0m \u001B[38;5;28;01mdef\u001B[39;00m \u001B[38;5;21m_post_retry_check_actions\u001B[39m(\u001B[38;5;28mself\u001B[39m, retry_state: \u001B[38;5;124m\"\u001B[39m\u001B[38;5;124mRetryCallState\u001B[39m\u001B[38;5;124m\"\u001B[39m) \u001B[38;5;241m-\u001B[39m\u001B[38;5;241m>\u001B[39m \u001B[38;5;28;01mNone\u001B[39;00m:\n\u001B[1;32m    389\u001B[0m     \u001B[38;5;28;01mif\u001B[39;00m \u001B[38;5;129;01mnot\u001B[39;00m (\u001B[38;5;28mself\u001B[39m\u001B[38;5;241m.\u001B[39miter_state\u001B[38;5;241m.\u001B[39mis_explicit_retry \u001B[38;5;129;01mor\u001B[39;00m \u001B[38;5;28mself\u001B[39m\u001B[38;5;241m.\u001B[39miter_state\u001B[38;5;241m.\u001B[39mretry_run_result):\n\u001B[0;32m--> 390\u001B[0m         \u001B[38;5;28mself\u001B[39m\u001B[38;5;241m.\u001B[39m_add_action_func(\u001B[38;5;28;01mlambda\u001B[39;00m rs: \u001B[43mrs\u001B[49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[43moutcome\u001B[49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[43mresult\u001B[49m\u001B[43m(\u001B[49m\u001B[43m)\u001B[49m)\n\u001B[1;32m    391\u001B[0m         \u001B[38;5;28;01mreturn\u001B[39;00m\n\u001B[1;32m    393\u001B[0m     \u001B[38;5;28;01mif\u001B[39;00m \u001B[38;5;28mself\u001B[39m\u001B[38;5;241m.\u001B[39mafter \u001B[38;5;129;01mis\u001B[39;00m \u001B[38;5;129;01mnot\u001B[39;00m \u001B[38;5;28;01mNone\u001B[39;00m:\n",
      "File \u001B[0;32m/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/concurrent/futures/_base.py:449\u001B[0m, in \u001B[0;36mFuture.result\u001B[0;34m(self, timeout)\u001B[0m\n\u001B[1;32m    447\u001B[0m     \u001B[38;5;28;01mraise\u001B[39;00m CancelledError()\n\u001B[1;32m    448\u001B[0m \u001B[38;5;28;01melif\u001B[39;00m \u001B[38;5;28mself\u001B[39m\u001B[38;5;241m.\u001B[39m_state \u001B[38;5;241m==\u001B[39m FINISHED:\n\u001B[0;32m--> 449\u001B[0m     \u001B[38;5;28;01mreturn\u001B[39;00m \u001B[38;5;28;43mself\u001B[39;49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[43m__get_result\u001B[49m\u001B[43m(\u001B[49m\u001B[43m)\u001B[49m\n\u001B[1;32m    451\u001B[0m \u001B[38;5;28mself\u001B[39m\u001B[38;5;241m.\u001B[39m_condition\u001B[38;5;241m.\u001B[39mwait(timeout)\n\u001B[1;32m    453\u001B[0m \u001B[38;5;28;01mif\u001B[39;00m \u001B[38;5;28mself\u001B[39m\u001B[38;5;241m.\u001B[39m_state \u001B[38;5;129;01min\u001B[39;00m [CANCELLED, CANCELLED_AND_NOTIFIED]:\n",
      "File \u001B[0;32m/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/concurrent/futures/_base.py:401\u001B[0m, in \u001B[0;36mFuture.__get_result\u001B[0;34m(self)\u001B[0m\n\u001B[1;32m    399\u001B[0m \u001B[38;5;28;01mif\u001B[39;00m \u001B[38;5;28mself\u001B[39m\u001B[38;5;241m.\u001B[39m_exception:\n\u001B[1;32m    400\u001B[0m     \u001B[38;5;28;01mtry\u001B[39;00m:\n\u001B[0;32m--> 401\u001B[0m         \u001B[38;5;28;01mraise\u001B[39;00m \u001B[38;5;28mself\u001B[39m\u001B[38;5;241m.\u001B[39m_exception\n\u001B[1;32m    402\u001B[0m     \u001B[38;5;28;01mfinally\u001B[39;00m:\n\u001B[1;32m    403\u001B[0m         \u001B[38;5;66;03m# Break a reference cycle with the exception in self._exception\u001B[39;00m\n\u001B[1;32m    404\u001B[0m         \u001B[38;5;28mself\u001B[39m \u001B[38;5;241m=\u001B[39m \u001B[38;5;28;01mNone\u001B[39;00m\n",
      "File \u001B[0;32m/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages/tenacity/__init__.py:470\u001B[0m, in \u001B[0;36mRetrying.__call__\u001B[0;34m(self, fn, *args, **kwargs)\u001B[0m\n\u001B[1;32m    468\u001B[0m \u001B[38;5;28;01mif\u001B[39;00m \u001B[38;5;28misinstance\u001B[39m(do, DoAttempt):\n\u001B[1;32m    469\u001B[0m     \u001B[38;5;28;01mtry\u001B[39;00m:\n\u001B[0;32m--> 470\u001B[0m         result \u001B[38;5;241m=\u001B[39m \u001B[43mfn\u001B[49m\u001B[43m(\u001B[49m\u001B[38;5;241;43m*\u001B[39;49m\u001B[43margs\u001B[49m\u001B[43m,\u001B[49m\u001B[43m \u001B[49m\u001B[38;5;241;43m*\u001B[39;49m\u001B[38;5;241;43m*\u001B[39;49m\u001B[43mkwargs\u001B[49m\u001B[43m)\u001B[49m\n\u001B[1;32m    471\u001B[0m     \u001B[38;5;28;01mexcept\u001B[39;00m \u001B[38;5;167;01mBaseException\u001B[39;00m:  \u001B[38;5;66;03m# noqa: B902\u001B[39;00m\n\u001B[1;32m    472\u001B[0m         retry_state\u001B[38;5;241m.\u001B[39mset_exception(sys\u001B[38;5;241m.\u001B[39mexc_info())  \u001B[38;5;66;03m# type: ignore[arg-type]\u001B[39;00m\n",
      "File \u001B[0;32m/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages/qianfan/resources/requestor/base.py:417\u001B[0m, in \u001B[0;36mBaseAPIRequestor._with_retry.<locals>._retry_wrapper\u001B[0;34m(*args)\u001B[0m\n\u001B[1;32m    406\u001B[0m \u001B[38;5;129m@retry\u001B[39m(\n\u001B[1;32m    407\u001B[0m     wait\u001B[38;5;241m=\u001B[39mwait_exponential_jitter(\n\u001B[1;32m    408\u001B[0m         initial\u001B[38;5;241m=\u001B[39mconfig\u001B[38;5;241m.\u001B[39mbackoff_factor,\n\u001B[0;32m   (...)\u001B[0m\n\u001B[1;32m    415\u001B[0m )\n\u001B[1;32m    416\u001B[0m \u001B[38;5;28;01mdef\u001B[39;00m \u001B[38;5;21m_retry_wrapper\u001B[39m(\u001B[38;5;241m*\u001B[39margs: Any) \u001B[38;5;241m-\u001B[39m\u001B[38;5;241m>\u001B[39m _T:\n\u001B[0;32m--> 417\u001B[0m     \u001B[38;5;28;01mreturn\u001B[39;00m \u001B[43mfunc\u001B[49m\u001B[43m(\u001B[49m\u001B[38;5;241;43m*\u001B[39;49m\u001B[43margs\u001B[49m\u001B[43m)\u001B[49m\n",
      "File \u001B[0;32m/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages/qianfan/resources/requestor/openapi_requestor.py:80\u001B[0m, in \u001B[0;36mQfAPIRequestor._retry_if_token_expired.<locals>.retry_wrapper\u001B[0;34m(*args, **kwargs)\u001B[0m\n\u001B[1;32m     78\u001B[0m \u001B[38;5;28;01mif\u001B[39;00m \u001B[38;5;129;01mnot\u001B[39;00m token_refreshed:\n\u001B[1;32m     79\u001B[0m     \u001B[38;5;28;01mtry\u001B[39;00m:\n\u001B[0;32m---> 80\u001B[0m         \u001B[38;5;28;01mreturn\u001B[39;00m \u001B[43mfunc\u001B[49m\u001B[43m(\u001B[49m\u001B[38;5;241;43m*\u001B[39;49m\u001B[43margs\u001B[49m\u001B[43m)\u001B[49m\n\u001B[1;32m     81\u001B[0m     \u001B[38;5;28;01mexcept\u001B[39;00m errors\u001B[38;5;241m.\u001B[39mAccessTokenExpiredError:\n\u001B[1;32m     82\u001B[0m         \u001B[38;5;66;03m# refresh token and set token_refreshed flag\u001B[39;00m\n\u001B[1;32m     83\u001B[0m         \u001B[38;5;28mself\u001B[39m\u001B[38;5;241m.\u001B[39m_auth\u001B[38;5;241m.\u001B[39mrefresh_access_token()\n",
      "File \u001B[0;32m/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages/qianfan/resources/requestor/openapi_requestor.py:413\u001B[0m, in \u001B[0;36mQfAPIRequestor.llm.<locals>._helper\u001B[0;34m()\u001B[0m\n\u001B[1;32m    407\u001B[0m     \u001B[38;5;28;01mreturn\u001B[39;00m \u001B[38;5;28mself\u001B[39m\u001B[38;5;241m.\u001B[39m_compensate_token_usage_stream(\n\u001B[1;32m    408\u001B[0m         \u001B[38;5;28mself\u001B[39m\u001B[38;5;241m.\u001B[39m_request_stream(req, data_postprocess\u001B[38;5;241m=\u001B[39mdata_postprocess),\n\u001B[1;32m    409\u001B[0m         token_count,\n\u001B[1;32m    410\u001B[0m     )\n\u001B[1;32m    411\u001B[0m \u001B[38;5;28;01melse\u001B[39;00m:\n\u001B[1;32m    412\u001B[0m     \u001B[38;5;28;01mreturn\u001B[39;00m \u001B[38;5;28mself\u001B[39m\u001B[38;5;241m.\u001B[39m_compensate_token_usage_non_stream(\n\u001B[0;32m--> 413\u001B[0m         \u001B[38;5;28;43mself\u001B[39;49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[43m_request\u001B[49m\u001B[43m(\u001B[49m\u001B[43mreq\u001B[49m\u001B[43m,\u001B[49m\u001B[43m \u001B[49m\u001B[43mdata_postprocess\u001B[49m\u001B[38;5;241;43m=\u001B[39;49m\u001B[43mdata_postprocess\u001B[49m\u001B[43m)\u001B[49m, token_count\n\u001B[1;32m    414\u001B[0m     )\n",
      "File \u001B[0;32m/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages/qianfan/resources/requestor/base.py:178\u001B[0m, in \u001B[0;36m_latency.<locals>.wrapper\u001B[0;34m(requestor, request, *args, **kwargs)\u001B[0m\n\u001B[1;32m    176\u001B[0m \u001B[38;5;28;01mwith\u001B[39;00m requestor\u001B[38;5;241m.\u001B[39m_rate_limiter:\n\u001B[1;32m    177\u001B[0m     start_time \u001B[38;5;241m=\u001B[39m time\u001B[38;5;241m.\u001B[39mperf_counter()\n\u001B[0;32m--> 178\u001B[0m     resp \u001B[38;5;241m=\u001B[39m \u001B[43mfunc\u001B[49m\u001B[43m(\u001B[49m\u001B[43mrequestor\u001B[49m\u001B[43m,\u001B[49m\u001B[43m \u001B[49m\u001B[43mrequest\u001B[49m\u001B[43m,\u001B[49m\u001B[43m \u001B[49m\u001B[38;5;241;43m*\u001B[39;49m\u001B[43margs\u001B[49m\u001B[43m,\u001B[49m\u001B[43m \u001B[49m\u001B[38;5;241;43m*\u001B[39;49m\u001B[38;5;241;43m*\u001B[39;49m\u001B[43mkwargs\u001B[49m\u001B[43m)\u001B[49m\n\u001B[1;32m    179\u001B[0m     resp\u001B[38;5;241m.\u001B[39mstatistic[\u001B[38;5;124m\"\u001B[39m\u001B[38;5;124mtotal_latency\u001B[39m\u001B[38;5;124m\"\u001B[39m] \u001B[38;5;241m=\u001B[39m time\u001B[38;5;241m.\u001B[39mperf_counter() \u001B[38;5;241m-\u001B[39m start_time\n\u001B[1;32m    180\u001B[0m     \u001B[38;5;28;01mreturn\u001B[39;00m resp\n",
      "File \u001B[0;32m/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages/qianfan/resources/requestor/base.py:311\u001B[0m, in \u001B[0;36mBaseAPIRequestor._request\u001B[0;34m(self, request, data_postprocess)\u001B[0m\n\u001B[1;32m    307\u001B[0m \u001B[38;5;28;01mexcept\u001B[39;00m requests\u001B[38;5;241m.\u001B[39mJSONDecodeError:\n\u001B[1;32m    308\u001B[0m     \u001B[38;5;28;01mraise\u001B[39;00m errors\u001B[38;5;241m.\u001B[39mRequestError(\n\u001B[1;32m    309\u001B[0m         \u001B[38;5;124mf\u001B[39m\u001B[38;5;124m\"\u001B[39m\u001B[38;5;124mGot invalid json response from server, body: \u001B[39m\u001B[38;5;132;01m{\u001B[39;00mresponse\u001B[38;5;241m.\u001B[39mcontent\u001B[38;5;132;01m!r}\u001B[39;00m\u001B[38;5;124m\"\u001B[39m\n\u001B[1;32m    310\u001B[0m     )\n\u001B[0;32m--> 311\u001B[0m resp \u001B[38;5;241m=\u001B[39m \u001B[38;5;28;43mself\u001B[39;49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[43m_parse_response\u001B[49m\u001B[43m(\u001B[49m\u001B[43mbody\u001B[49m\u001B[43m,\u001B[49m\u001B[43m \u001B[49m\u001B[43mresponse\u001B[49m\u001B[43m)\u001B[49m\n\u001B[1;32m    312\u001B[0m resp\u001B[38;5;241m.\u001B[39mstatistic[\u001B[38;5;124m\"\u001B[39m\u001B[38;5;124mrequest_latency\u001B[39m\u001B[38;5;124m\"\u001B[39m] \u001B[38;5;241m=\u001B[39m response\u001B[38;5;241m.\u001B[39melapsed\u001B[38;5;241m.\u001B[39mtotal_seconds()\n\u001B[1;32m    313\u001B[0m resp\u001B[38;5;241m.\u001B[39mrequest \u001B[38;5;241m=\u001B[39m QfRequest\u001B[38;5;241m.\u001B[39mfrom_requests(response\u001B[38;5;241m.\u001B[39mrequest)\n",
      "File \u001B[0;32m/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages/qianfan/resources/requestor/openapi_requestor.py:276\u001B[0m, in \u001B[0;36mQfAPIRequestor._parse_response\u001B[0;34m(self, body, resp)\u001B[0m\n\u001B[1;32m    274\u001B[0m \u001B[38;5;28;01mif\u001B[39;00m e\u001B[38;5;241m.\u001B[39merror_code \u001B[38;5;241m==\u001B[39m APIErrorCode\u001B[38;5;241m.\u001B[39mTPMLimitReached\u001B[38;5;241m.\u001B[39mvalue:\n\u001B[1;32m    275\u001B[0m     \u001B[38;5;28mself\u001B[39m\u001B[38;5;241m.\u001B[39m_token_limiter\u001B[38;5;241m.\u001B[39mclear()\n\u001B[0;32m--> 276\u001B[0m \u001B[38;5;28;01mraise\u001B[39;00m e\n",
      "File \u001B[0;32m/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages/qianfan/resources/requestor/openapi_requestor.py:272\u001B[0m, in \u001B[0;36mQfAPIRequestor._parse_response\u001B[0;34m(self, body, resp)\u001B[0m\n\u001B[1;32m    268\u001B[0m \u001B[38;5;28;01mdef\u001B[39;00m \u001B[38;5;21m_parse_response\u001B[39m(\n\u001B[1;32m    269\u001B[0m     \u001B[38;5;28mself\u001B[39m, body: Dict[\u001B[38;5;28mstr\u001B[39m, Any], resp: requests\u001B[38;5;241m.\u001B[39mResponse\n\u001B[1;32m    270\u001B[0m ) \u001B[38;5;241m-\u001B[39m\u001B[38;5;241m>\u001B[39m QfResponse:\n\u001B[1;32m    271\u001B[0m     \u001B[38;5;28;01mtry\u001B[39;00m:\n\u001B[0;32m--> 272\u001B[0m         \u001B[38;5;28;01mreturn\u001B[39;00m \u001B[38;5;28;43msuper\u001B[39;49m\u001B[43m(\u001B[49m\u001B[43m)\u001B[49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[43m_parse_response\u001B[49m\u001B[43m(\u001B[49m\u001B[43mbody\u001B[49m\u001B[43m,\u001B[49m\u001B[43m \u001B[49m\u001B[43mresp\u001B[49m\u001B[43m)\u001B[49m\n\u001B[1;32m    273\u001B[0m     \u001B[38;5;28;01mexcept\u001B[39;00m errors\u001B[38;5;241m.\u001B[39mAPIError \u001B[38;5;28;01mas\u001B[39;00m e:\n\u001B[1;32m    274\u001B[0m         \u001B[38;5;28;01mif\u001B[39;00m e\u001B[38;5;241m.\u001B[39merror_code \u001B[38;5;241m==\u001B[39m APIErrorCode\u001B[38;5;241m.\u001B[39mTPMLimitReached\u001B[38;5;241m.\u001B[39mvalue:\n",
      "File \u001B[0;32m/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages/qianfan/resources/requestor/base.py:362\u001B[0m, in \u001B[0;36mBaseAPIRequestor._parse_response\u001B[0;34m(self, body, resp)\u001B[0m\n\u001B[1;32m    356\u001B[0m \u001B[38;5;28;01mdef\u001B[39;00m \u001B[38;5;21m_parse_response\u001B[39m(\n\u001B[1;32m    357\u001B[0m     \u001B[38;5;28mself\u001B[39m, body: Dict[\u001B[38;5;28mstr\u001B[39m, Any], resp: requests\u001B[38;5;241m.\u001B[39mResponse\n\u001B[1;32m    358\u001B[0m ) \u001B[38;5;241m-\u001B[39m\u001B[38;5;241m>\u001B[39m QfResponse:\n\u001B[1;32m    359\u001B[0m \u001B[38;5;250m    \u001B[39m\u001B[38;5;124;03m\"\"\"\u001B[39;00m\n\u001B[1;32m    360\u001B[0m \u001B[38;5;124;03m    parse response to QfResponse\u001B[39;00m\n\u001B[1;32m    361\u001B[0m \u001B[38;5;124;03m    \"\"\"\u001B[39;00m\n\u001B[0;32m--> 362\u001B[0m     \u001B[38;5;28;43mself\u001B[39;49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[43m_check_error\u001B[49m\u001B[43m(\u001B[49m\u001B[43mbody\u001B[49m\u001B[43m)\u001B[49m\n\u001B[1;32m    363\u001B[0m     qf_response \u001B[38;5;241m=\u001B[39m QfResponse(\n\u001B[1;32m    364\u001B[0m         code\u001B[38;5;241m=\u001B[39mresp\u001B[38;5;241m.\u001B[39mstatus_code, headers\u001B[38;5;241m=\u001B[39m\u001B[38;5;28mdict\u001B[39m(resp\u001B[38;5;241m.\u001B[39mheaders), body\u001B[38;5;241m=\u001B[39mbody\n\u001B[1;32m    365\u001B[0m     )\n\u001B[1;32m    366\u001B[0m     \u001B[38;5;28;01mreturn\u001B[39;00m qf_response\n",
      "File \u001B[0;32m/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages/qianfan/resources/requestor/openapi_requestor.py:266\u001B[0m, in \u001B[0;36mQfAPIRequestor._check_error\u001B[0;34m(self, body)\u001B[0m\n\u001B[1;32m    261\u001B[0m \u001B[38;5;28;01mif\u001B[39;00m error_code \u001B[38;5;129;01min\u001B[39;00m {\n\u001B[1;32m    262\u001B[0m     APIErrorCode\u001B[38;5;241m.\u001B[39mAPITokenExpired\u001B[38;5;241m.\u001B[39mvalue,\n\u001B[1;32m    263\u001B[0m     APIErrorCode\u001B[38;5;241m.\u001B[39mAPITokenInvalid\u001B[38;5;241m.\u001B[39mvalue,\n\u001B[1;32m    264\u001B[0m }:\n\u001B[1;32m    265\u001B[0m     \u001B[38;5;28;01mraise\u001B[39;00m errors\u001B[38;5;241m.\u001B[39mAccessTokenExpiredError\n\u001B[0;32m--> 266\u001B[0m \u001B[38;5;28;01mraise\u001B[39;00m errors\u001B[38;5;241m.\u001B[39mAPIError(error_code, err_msg, req_id)\n",
      "\u001B[0;31mAPIError\u001B[0m: api return error, req_id:  code: 17, msg: Open api daily request limit reached 可能的原因: 未开通所调用服务的付费权限，或者账户已欠费"
     ]
    }
   ],
   "source": [
    "from langchain.prompts.example_selector import SemanticSimilarityExampleSelector\n",
    "from langchain.vectorstores import FAISS\n",
    "from langchain.embeddings import QianfanEmbeddingsEndpoint\n",
    "from langchain.prompts import FewShotPromptTemplate,PromptTemplate\n",
    "from langchain.llms.baidu_qianfan_endpoint import QianfanLLMEndpoint\n",
    "\n",
    "llm = QianfanLLMEndpoint(model = \"Qianfan-Chinese-Llama-2-7B\")\n",
    "# 创建一个示例列表\n",
    "# 自动客服的系统。 \n",
    "examples = [\n",
    "    {\"input\": \"我忘记了密码，怎么重置？\", \"output\": \"账户问题\"},\n",
    "    {\"input\": \"我的订单在哪里，已经过了一周了？\", \"output\": \"订单问题\"},\n",
    "    {\"input\": \"我收到了一个有缺陷的物品，我怎么退货？\", \"output\": \"退货和退款问题\"},\n",
    "    {\"input\": \"我想更改我的送货地址。\", \"output\": \"订单问题\"},\n",
    "    {\"input\": \"我怎么升级我的会员资格？\", \"output\": \"账户问题\"},\n",
    "    {\"input\": \"我的订单被收了两次费，我该怎么办？\", \"output\": \"账单问题\"},\n",
    "    {\"input\": \"送货的商品不是我订购的，我怎么换货？\", \"output\": \"退货和退款问题\"},\n",
    "    {\"input\": \"我怎么取消我的订单？\", \"output\": \"订单问题\"},\n",
    "    {\"input\": \"我的优惠码不起作用，你能帮帮我吗？\", \"output\": \"促销问题\"},\n",
    "    {\"input\": \"我无法登录我的账户，我该怎么办？\", \"output\": \"账户问题\"}\n",
    "]\n",
    "\n",
    "example_prompt = PromptTemplate(\n",
    "    input_variables= [\"input\", \"output\"],\n",
    "    template= \"示例输入:{input}\\n 示例输出:{output}\",\n",
    ")\n",
    "\n",
    "exmaple_selector = SemanticSimilarityExampleSelector.from_examples(\n",
    "    examples,\n",
    "    QianfanEmbeddingsEndpoint(),\n",
    "    FAISS,\n",
    "    k=2\n",
    ")\n",
    "\n",
    "similar_prompt=FewShotPromptTemplate(\n",
    "    example_selector= exmaple_selector,\n",
    "    example_prompt = example_prompt,\n",
    "    prefix=\"以下是一个客户服务查询，请将其分类：\",\n",
    "    suffix=\"输入：{query}\\n输出：\",\n",
    "    input_variables=[\"query\"],\n",
    ")\n",
    "my_query = \"我想知道我的订单何时能够到达？\"\n",
    "print (similar_prompt.format(query=my_query))\n",
    "\n",
    "response = llm.generate([str(similar_prompt.format(query=my_query))])\n",
    "print(response)\n"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-05-26T02:45:09.336211Z",
     "start_time": "2024-05-26T02:45:08.103038Z"
    }
   },
   "id": "306bb0495f4c7daa",
   "execution_count": 3
  },
  {
   "cell_type": "markdown",
   "source": [],
   "metadata": {
    "collapsed": false
   },
   "id": "2d73005f58a12e68"
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "华为P40评价\n",
      "By 用户A\n",
      "\n",
      "我非常喜欢这款手机，摄像头非常好。5星满分。\n",
      "\n",
      "{\n",
      "  \"产品\": \"华为P40\",\n",
      "  \"情感\": \"正面\",\n",
      "  \"评价\": 5\n",
      "}\n",
      "\n",
      "---------------\n",
      "\n",
      "三星Galaxy评价\n",
      "By 用户B\n",
      "\n",
      "电池续航令人失望。给2星。\n",
      "\n",
      "{\n",
      "  \"产品\": \"三星Galaxy\",\n",
      "  \"情感\": \"负面\",\n",
      "  \"评价\": 2\n",
      "}\n"
     ]
    }
   ],
   "source": [
    "from langchain.schema import Document\n",
    "from langchain.chat_models import ChatOpenAI\n",
    "from langchain.document_transformers.openai_functions import create_metadata_tagger\n",
    "import langchain.cache\n",
    "schema = {\n",
    "    \"properties\":{\n",
    "        \"产品\" : {\"type\":\"string\"},\n",
    "        \"情感\": {\"type\":\"string\",\"enum\":[\"正面\",\"负面\"]},\n",
    "        \"评价\": {\"type\":\"integer\",\"description\":\"用户给出的评分，满分为5\"}\n",
    "    },\n",
    "    \"required\":[\"产品\",\"情感\"]\n",
    "}\n",
    "\n",
    "llm = ChatOpenAI(model = \"gpt-3.5-turbo\")\n",
    "# 创建元数据标签生成器\n",
    "document_transformer = create_metadata_tagger(metadata_schema=schema, llm=llm)\n",
    "\n",
    "# 定义原始文档\n",
    "original_document = [\n",
    "     Document(\n",
    "        page_content=\"华为P40评价\\nBy 用户A\\n\\n我非常喜欢这款手机，摄像头非常好。5星满分。\"\n",
    "    ),\n",
    "    Document(\n",
    "        page_content=\"三星Galaxy评价\\nBy 用户B\\n\\n电池续航令人失望。给2星。\"\n",
    "    ),\n",
    "]\n",
    "\n",
    "# 转换文档\n",
    "enhanced_documents = document_transformer.transform_documents(original_document)\n",
    "\n",
    "# 打印增强的文档\n",
    "import json\n",
    "print(\n",
    "    *[\n",
    "      d.page_content + \"\\n\\n\" + json.dumps(d.metadata, ensure_ascii=False, indent=2)  for d in enhanced_documents\n",
    "    ],\n",
    "    sep=\"\\n\\n---------------\\n\\n\"\n",
    ")\n",
    "\n",
    "\n",
    "\n"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-05-26T02:44:58.700450Z",
     "start_time": "2024-05-26T02:44:54.565321Z"
    }
   },
   "id": "fa19fb43dbed6ae0",
   "execution_count": 2
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['该款智能手机配备了一块6.7英寸的超清液晶显示屏，分辨率高达3200x1440，显示效果极为出色。搭载了高通骁龙888处理器，运行速度非常快，能够流畅运行大多数应用和游戏。内置5000mAh大容量电', '数应用和游戏。内置5000mAh大容量电池，续航能力强，支持快充技术，能在短时间内充满电池。拥有128GB的内部存储空间，可以存储大量的应用、照片和视频，同时支持扩展存储卡，最大可以扩展到1TB。后置', '持扩展存储卡，最大可以扩展到1TB。后置有4800万像素的高清摄像头，支持8K视频录制和超清拍照，前置有2000万像素的自拍摄像头，自拍效果非常好。支持5G网络，下载速度非常快，网络信号稳定。还有许多', '，下载速度非常快，网络信号稳定。还有许多其他功能，如面部解锁、指纹识别、防水防尘等，为用户提供了极为便捷的使用体验。']\n",
      "该款智能手机配备了一块6.7英寸的超清液晶显示屏，分辨率高达3200x1440，显示效果极为出色。搭载了高通骁龙888处理器，运行速度非常快，能够流畅运行大多数应用和游戏。内置5000mAh大容量电\n"
     ]
    }
   ],
   "source": [
    "from langchain.embeddings import OpenAIEmbeddings\n",
    "from langchain.text_splitter import RecursiveCharacterTextSplitter\n",
    "from langchain.vectorstores import FAISS\n",
    "\n",
    "# Replace the raw documents part with the given string\n",
    "raw_documents = \"\"\"\n",
    "该款智能手机配备了一块6.7英寸的超清液晶显示屏，分辨率高达3200x1440，显示效果极为出色。搭载了高通骁龙888处理器，运行速度非常快，能够流畅运行大多数应用和游戏。内置5000mAh大容量电池，续航能力强，支持快充技术，能在短时间内充满电池。拥有128GB的内部存储空间，可以存储大量的应用、照片和视频，同时支持扩展存储卡，最大可以扩展到1TB。后置有4800万像素的高清摄像头，支持8K视频录制和超清拍照，前置有2000万像素的自拍摄像头，自拍效果非常好。支持5G网络，下载速度非常快，网络信号稳定。还有许多其他功能，如面部解锁、指纹识别、防水防尘等，为用户提供了极为便捷的使用体验。\n",
    "\"\"\"\n",
    "\n",
    "# 实例化RecursiveCharacterTextSplitter，块大小为100字符，重叠区域为20字符\n",
    "text_splitter = RecursiveCharacterTextSplitter(\n",
    "    chunk_size = 100,\n",
    "    chunk_overlap = 20,\n",
    "    length_function = len\n",
    ")\n",
    "\n",
    "documents = text_splitter.split_text(text = raw_documents)\n",
    "print(documents)\n",
    "vectorStore = FAISS.from_texts(documents, OpenAIEmbeddings())\n",
    "\n",
    "query = \"手机分辨率如何\"\n",
    "#查询已经存储向量的手机描述信息\n",
    "docs = vectorStore.similarity_search(query)\n",
    "\n",
    "print(docs[0].page_content)\n",
    "\n",
    "\n"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-05-26T03:59:57.365217Z",
     "start_time": "2024-05-26T03:59:56.161685Z"
    }
   },
   "id": "df4535df438a46be",
   "execution_count": 6
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
